package com.iweb.test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * ClassName: HelloCookie
 * Package: com.iweb.test
 * Description:
 *
 * @Author Jerry
 * @Create 2024/8/7 8:40
 * @Version 1.0
 */
@WebServlet(value = "/helloCookie")
public class HelloCookie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies=req.getCookies();
        if(cookies!=null && cookies.length>0){
            for(Cookie cookie:cookies){
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }else{
            System.out.println("没有cookie，您是第一次访问当前服务器");
            HttpSession session=req.getSession();
            Cookie cookie1=new Cookie("myKey","myValue");
            cookie1.setMaxAge(30);
            resp.addCookie(cookie1);
        }
    }
}
